package com.example.day1_dialog;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {


    private ListView lv;
    MusicAdapter musicAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        musicAdapter = new MusicAdapter();
        lv.setAdapter(musicAdapter);
        String singer = "邓紫棋";
        HttpUtils.doGet("http://10.161.9.80:7004/song/json",new HashMap<String,String>(){{put("singer",singer);}},MusicEntity.class,r->{
            musicAdapter.addAll(r.getData());
        },t->{t.printStackTrace();});
        //点击item
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //musicAdapter.remove(position);
                showMenu(position,view);
            }
        });
        //长按item
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                return false;
            }
        });

    }

    private void showMenu(int position, View view) {
        //实例化 popupMenu
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载资源
        popupMenu.inflate(R.menu.option_menu);
        //点击菜单
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.item1:
                        Toast.makeText(MainActivity.this, "点击了添加好友菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item2:
                        Toast.makeText(MainActivity.this, "点击了扫一扫菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item3:
                        Toast.makeText(MainActivity.this, "点击了二维码菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item31:
                        Toast.makeText(MainActivity.this, "点击了二维码1菜单", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.item32:
                        Toast.makeText(MainActivity.this, "点击了二维码2菜单", Toast.LENGTH_SHORT).show();
                        break;

                }
                return false;
            }
        });
        //显示菜单
        popupMenu.show();
    }

    //普通对话框
    public void normal(View view) {
        //创建构建者
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置属性
        builder.setTitle("这是标题");
        builder.setMessage("这是内容");
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "点击了确定按钮", Toast.LENGTH_SHORT).show();
            }
        });

        builder.setIcon(R.mipmap.ic_launcher);
        //创建对话框
        AlertDialog dialog = builder.create();
        //显示对话框
        dialog.show();
        //dialog.dismiss();//对话框消失
    }

    //单选对话框--链式编程
    int index = 0;

    public void single(View view) {
        String[] items = new String[]{"男", "女", "不详"};
        new AlertDialog.Builder(this)
                .setTitle("单选对话框")
                //参数一：选项内容  参数二：默认选中的数组的下标  参数三：响应事件
                .setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "选中的是" + items[which], Toast.LENGTH_SHORT).show();
                        index = which;
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                        Toast.makeText(MainActivity.this, "最终选择了" + items[index], Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    //多选对话框
    public void mutli(View view) {
        String[] items = new String[]{"北京", "上海", "深圳", "广州"};
        boolean[] flags = new boolean[]{true, true, false, false};
        new AlertDialog.Builder(this)
                .setTitle("多选对话框")
                .setMultiChoiceItems(items, flags, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        //选中深圳的情况，对应flags中的第三个就要修改成true
                        flags[which] = isChecked;
                        Toast.makeText(MainActivity.this, items[which] + "当前的状态为" + flags[which] + "或者可以显示" + isChecked, Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "点击了取消按钮", Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = "";
                        //循环标志数组，选择其中被选中的进行展示
                        for (int i = 0; i < flags.length; i++) {
                            if (flags[i]) {
                                s = s + items[i] + ",";
                            }
                        }
                        Toast.makeText(MainActivity.this, "最终选择的是" + s, Toast.LENGTH_SHORT).show();
                    }
                })
                .show();
    }

    //日期选择器
    public void date(View view) {
        //日历对象
        Calendar calendar = Calendar.getInstance();
        //日期选择对话框
        //参数一：上下文  参数二：响应事件  参数三：默认显示的年  参数四：默认显示的月  参数五：默认显示的日
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                //默认显示月份的时候要进行+1,
                Toast.makeText(MainActivity.this, "选中的日期是" + year + "-" + (month + 1) + "-" + dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH))
                .show();
    }

    //时间选择对话框
    public void time(View view) {
        Calendar calendar = Calendar.getInstance();
        //参数一：上下文 参数二响应事件 参数三 默认显示的消逝 参数四：默认显示的分钟  参数五 ：是否是24小时的显示样式
        new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                Toast.makeText(MainActivity.this, "选中的时间为" + hourOfDay + ":" + minute, Toast.LENGTH_SHORT).show();
            }
        }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE), false)
                .show();

    }

    Timer timer;//定时器

    public void progress(View view) {
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("下载");
        dialog.setMessage("正在下载中，请稍后。。。");
        dialog.setIcon(R.mipmap.ic_launcher);
        dialog.setMax(100);
        dialog.setProgress(0);//结合定时器时，不起作用
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//STYLE_HORIZONTAL 水平进度条  STYLE_SPINNER圆形进度条
        dialog.show();
        timer = new Timer();
        //schedule  有规划，计划的意思 TimerTask:做的具体事情 0：代表几秒后开始执行，1000 多长时间重复一次   0和1000都是毫秒级别的
        timer.schedule(new TimerTask() {
            int progress = 0;//最开始的进度为0

            @Override
            public void run() {
                if (progress == 100) {
                    timer.cancel();//定时器关闭
                    dialog.dismiss();//对话框关闭、消失
                }
                dialog.setProgress(progress += 20);
            }
        }, 0, 1000);

    }

    //自定义对话框
    AlertDialog dialog;

    public void diy(View view) {
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_diy, null);
        TextView tv = inflate.findViewById(R.id.tv);
        ImageView iv = inflate.findViewById(R.id.iv);
        Button btn_yes = inflate.findViewById(R.id.btn_yes);
        Button btn_no = inflate.findViewById(R.id.btn_no);
        tv.setText("图片");
        //iv.setImageResource(R.drawable.ke);//本地图片
        //网络图片
        Glide.with(this).load("https://www.wanandroid.com/blogimgs/42da12d8-de56-4439-b40c-eab66c227a4b.png").into(iv);
        btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "这个图片有点乱啊", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
        btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "这个图片不好看啊", Toast.LENGTH_SHORT).show();
                dialog.dismiss();//关闭对话框
            }
        });
        dialog = new AlertDialog.Builder(this)
                .setView(inflate)
                .create();
        dialog.show();
    }

    private void initView() {
        lv = (ListView) findViewById(R.id.lv);
    }
}